package mysql

import (
	"gorm.io/gorm"
	"work/workrpc/workrpc"
)

type User struct {
	gorm.Model
	Phone     string `gorm:"type:varchar(30);comment:手机号"`
	Password  string `gorm:"type:varchar(32);comment:密码"`
	HeadImage string `gorm:"type:varchar(255);comment:头像"`
	NickName  string `gorm:"type:varchar(30);comment:昵称"`
}

func (u *User) UserRegister(db *gorm.DB) error {
	return db.Debug().Create(u).Error
}

func (u *User) FindUserPhone(db *gorm.DB, phone string) error {
	return db.Debug().Where("phone=?", phone).First(u).Error
}

func (u *User) UpdatePassword(db *gorm.DB) error {
	return db.Debug().Updates(u).Error
}
func UserAndContent(db *gorm.DB, in *workrpc.ContentShowRequest) ([]*workrpc.UserAndContent, error) {
	var list []*workrpc.UserAndContent

	//SELECT
	//  nick_name,
	//  head_image,
	//  `comment`
	//FROM
	//  users
	//  LEFT JOIN contents ON users.id = contents.user_id
	db.Debug().Model(&User{}).Select(
		"nick_name",
		"head_image",
		"comment",
	).Joins("LEFT JOIN contents ON users.id = contents.user_id").
		Where("contents.story_id=?", in.StoryId).
		Order("contents.created_at desc").
		Find(&list)
	return list, err
}
